﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DB
/// </summary>
public class DB
{
	public DB()
	{

	}

    public static List<String> selectQuery(String connStringNome, String query)
    {
        List<String>results = new List<string>();
        String tmp ="";
        String connectionString = ConfigurationManager.ConnectionStrings[connStringNome].ToString();
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    results.Add(reader.GetValue(0).ToString());
                }
                reader.Close();
                connection.Close();
            }
        }
        var prova = results;
        return results;
    
	}

    public static void insertQuery(String connStringNome, String query, Dictionary<String, String> parametri)
    {
        string connectionString = ConfigurationManager.ConnectionStrings[connStringNome].ToString();
        using (SqlConnection connection = new SqlConnection(connectionString))
        {

                SqlCommand cmd = new SqlCommand(query);

                foreach (var tmp in parametri)
                {
                    cmd.Parameters.Add("@"+tmp.Key, tmp.Value);
                }
                cmd.CommandType = CommandType.Text;
                cmd.Connection = connection;
                connection.Open();
                cmd.ExecuteNonQuery();
        }
    }
    public static void insertWithOutPar(String connStringNome, String query)
    {
        string connectionString = ConfigurationManager.ConnectionStrings[connStringNome].ToString();
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand(query);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = connection;
            connection.Open();
            cmd.ExecuteNonQuery();
        }
    }
}

 //foreach (var tmp in parametri)
            //    {
               //       command.Parameters.Add("@" + tmp.Key , tmp.Value);
//      }

//, Dictionary<String, String> parametri